package com.shimao.celnet.payment.controller;

import com.shimao.celnet.payment.form.PInvoiceBlueForm;
import com.shimao.celnet.payment.model.PInvoiceBlue;
import com.shimao.celnet.payment.model.PInvoiceBlueDetail;
import com.shimao.celnet.payment.model.PInvoiceRed;
import com.shimao.celnet.payment.service.BEstateService;
import com.shimao.celnet.payment.service.PInvoiceBlueService;
import com.shimao.celnet.payment.service.PInvoiceRedService;
import com.shimao.celnet.payment.service.SCodeNameService;
import com.shimao.celnet.payment.util.FinalUtil;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * 发票记录查询 控制类
 * 1、根据条件一览查询： PInvoiceBlueForm（查询条件，蓝票详情，蓝票list，蓝票明细list，红票list）
 * 2、一览查询界面，点击详情，通过发票guid，查看开票详情与明细  PInvoiceBlueForm
 * Created by ensure on 2018/01/17.
 *
 */
@Controller
@RequestMapping("/blueInvoices")
public class PInvoiceBlueController {
    protected  Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private SCodeNameService sCodeNameService;

    @Autowired
    private BEstateService bEstateService;

    @Autowired
    private PInvoiceBlueService pInvoiceBlueService;

    @Autowired
    private PInvoiceRedService pInvoiceRedService;

    /**
     * 根据条件查询开票信息列表（蓝票）
     * @param formBean
     * @param pageActionNm
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String getPageList(@ModelAttribute PInvoiceBlueForm formBean, String pageActionNm, Model model){
        logger.info("查询列表页面--开始");

        if(StringUtils.isNotEmpty(formBean.getGuid())){
            //更新，新增后，重新刷新情况，查询条件清空
            formBean=new PInvoiceBlueForm();
        }

        //当前页码设定
        int pageNum=FinalUtil.PAGE_Fist_NUM;

        if(formBean.isIsLastPage()){
            pageNum=formBean.getPages();
        }

        if(!StringUtils.isEmpty(pageActionNm) && !"0".equals(pageActionNm)){
            pageNum=Integer.valueOf(pageActionNm);
        }


        //页面数据查询结果
        formBean = pInvoiceBlueService.getByPage(formBean, pageNum, FinalUtil.PAGE_LENGHT);

        //小区：列表取得
        formBean.setEstateList(bEstateService.getAll());

        //业务类型取得
        formBean.setBusinessTypeList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_BUSINESS_TYPE));


        //发票种类取得：专用发票、普通发票、电子发票、卷式发票
        formBean.setInvoiceKindList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_INVOICE_KIND));

        //支付状态取得：开票申请、开票成功、开票失败
        formBean.setInvoiceStatusList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_INVOICE_STATUS));
        formBean.setRedFlg(formBean.getRedFlg());//是否红冲
        formBean.setInvoicePrintFlg(formBean.getInvoicePrintFlg());//是否打印
        formBean.setInvlidFlg(formBean.getInvlidFlg());//是否作废
        formBean.setSelStartTime(MyDateUtil.getPreOneDayYmd());//获取当前日期的前1天(yyyy-MM-dd)
        formBean.setSelEndTime(MyDateUtil.getToDayYmd());//当前日期(yyyy-MM-dd)
        model.addAttribute("formBean", formBean);

        logger.info("查询列表页面--结束");

        return "invoice/invoiceList";
    }


    /**
     * 详情初始化
     * @param
     * @return
     */
    @RequestMapping("/info")
    public  String  getInfo(@RequestParam String guid, Model model) {
        logger.info("详情页面--开始");
        PInvoiceBlueForm formBean = new PInvoiceBlueForm();
        //通过开票guid，查询蓝票记录详情，蓝票明细，红票记录列表
        Map<String,Object> retMap = pInvoiceBlueService.getInfo(guid);

        //1、蓝票详情
        PInvoiceBlue blueBean = (PInvoiceBlue)retMap.get("pInvoiceBlue");
        formBean.setGuid(blueBean.getGuid());//记录 GUID
        formBean.setImmediatelyFlg(blueBean.getImmediatelyFlg());//即时开票
        formBean.setBusinessType(blueBean.getBusinessType());//支付业务类型
        formBean.setEstateSfGuid(blueBean.getEstateSfGuid());//小区SF-GUID
        formBean.setDeviceType(blueBean.getDeviceType());//设备类型
        formBean.setInvoiceTypeCode(blueBean.getInvoiceTypeCode());//发票种类编码
        formBean.setOrganizationCode(blueBean.getOrganizationCode());//组织机构代码
        formBean.setSerialNo(blueBean.getSerialNo());//开票流水号
        formBean.setInvoiceSpecialMark(blueBean.getInvoiceSpecialMark());//特殊票种标记
        formBean.setSellerTaxNo(blueBean.getSellerTaxNo());//销方单位税号
        formBean.setInvoiceYerminalCode(blueBean.getInvoiceYerminalCode());//开票点编码
        formBean.setBuyerTaxNo(blueBean.getBuyerTaxNo());//购方单位税号
        formBean.setBuyerName(blueBean.getBuyerName());//购方单位名称
        formBean.setBuyerAddressPhone(blueBean.getBuyerAddressPhone());//购方地址及电话
        formBean.setBuyerBankAccount(blueBean.getBuyerBankAccount());//购方开户行及账号
        formBean.setBuyerPhone(blueBean.getBuyerPhone());//购方电话
        formBean.setBuyerEmail(blueBean.getBuyerEmail());//购方邮箱
        formBean.setDrawer(blueBean.getDrawer());//开票人
        formBean.setChecker(blueBean.getChecker());//复核人
        formBean.setPayee(blueBean.getPayee());//收款人
        formBean.setInvoiceListMark(blueBean.getInvoiceListMark());//清单标志
        formBean.setRedInfoNo(blueBean.getRedInfoNo());//红字信息表编号
        formBean.setTaxationMode(blueBean.getTaxationMode());//征税方式
        formBean.setDeductibleAmount(blueBean.getDeductibleAmount());//扣除额
        formBean.setInvoiceTotalPrice(blueBean.getInvoiceTotalPrice());//合计金额
        formBean.setInvoiceTotalTax(blueBean.getInvoiceTotalTax());//合计税额
        formBean.setInvoiceTotalPriceTax(blueBean.getInvoiceTotalPriceTax());//蓝票-价税合计（开蓝票金额）
        formBean.setInvlidFlg(blueBean.getInvlidFlg());//作废标志
        formBean.setInvlidTime(blueBean.getInvlidTime());//作废时间
        formBean.setRedFlg(blueBean.getRedFlg());//红冲标志
        formBean.setRedInvoiceTotalPriceTax(blueBean.getRedInvoiceTotalPriceTax());//红冲-价税合计（开红票金额）
        formBean.setEnRedAmount(blueBean.getEnRedAmount());//可红冲金额合计；
        formBean.setSignatureParameter(blueBean.getSignatureParameter());//签名值参数
        formBean.setTaxDiskNo(blueBean.getTaxDiskNo());//税控盘编号
        formBean.setTaxDiskKey(blueBean.getTaxDiskKey());//税控盘口令
        formBean.setTaxDiskPassword(blueBean.getTaxDiskPassword());//税务数字证书密码
//        formBean.setGoodsCodeVersion(blueBean.getGoodsCodeVersion());//商品编码版本号
//        formBean.setConsolidatedTaxRate(blueBean.getConsolidatedTaxRate());//综合税率
//        formBean.setnotificationNo(blueBean.getnotificationNo());//通知单编号
        formBean.setRemarks(blueBean.getRemarks());//备注
        formBean.setInvoiceCode(blueBean.getInvoiceCode());//发票代码
        formBean.setInvoiceNo(blueBean.getInvoiceNo());//发票号码
        formBean.setInvoiceStartTime(blueBean.getInvoiceStartTime());//开票开始时间
        formBean.setInvoiceEndTime(blueBean.getInvoiceEndTime());//开票完成时间
        formBean.setTaxControlCode(blueBean.getTaxControlCode());//税控码
        formBean.setInvoiceCheckCode(blueBean.getInvoiceCheckCode());//校验码
        formBean.setInvoiceQrcode(blueBean.getInvoiceQrcode());//二维码
        formBean.setInvoicePrintType(blueBean.getInvoicePrintType());//打印类型
//        formBean.setInvoicePrintMode(blueBean.getInvoicePrintMode());//打印方式
        formBean.setInvoicePrintTime(blueBean.getInvoicePrintTime());//打印日期
        formBean.setInvoicePrintFlg(blueBean.getInvoicePrintFlg());//打印标志
        formBean.setInvoiceStatus(blueBean.getInvoiceStatus());//开票状态
        formBean.setAppKey(blueBean.getAppKey());//应用key
        formBean.setInterfaceId(blueBean.getInterfaceId());//接口编号
        formBean.setInvoiceUrl(blueBean.getInvoiceUrl());//电子发票下载地址

        //2、蓝票详情的开票明细
        List<PInvoiceBlueDetail> blueDetailList =  (List<PInvoiceBlueDetail>)retMap.get("pInvoiceDetailList");
        formBean.setpInvoiceBlueDetails(blueDetailList);

        //3、蓝票详情对应的红冲list
        List<PInvoiceRed> blueBeanList =  (List<PInvoiceRed>)retMap.get("pInvoiceRedList");
        formBean.setpInvoiceRedList(blueBeanList);

        //小区：列表取得
        formBean.setEstateList(bEstateService.getAll());

        //业务类型取得
        formBean.setBusinessTypeList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_BUSINESS_TYPE));

        //发票种类取得：专用发票、普通发票、电子发票、卷式发票
        formBean.setInvoiceKindList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_INVOICE_KIND));

        //支付状态取得：开票申请、开票成功、开票失败
        formBean.setInvoiceStatusList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_INVOICE_STATUS));

        model.addAttribute("formBean",formBean);
        logger.info("详情页面--结束");
        return "invoice/invoiceBlueInfo";
    }


}
